package cn.ffcs.uec.organization.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.organization.action.bean.UecOrganizationMainComposerBean;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.staffAndOutStaff.manager.StaffAndOutStaffManager;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class uecOrganizationMainComposer extends BasePortletComposer {

    /**
     * 
     */
    private static final long serialVersionUID = -3678102540226992591L;

    private UecOrganizationMainComposerBean bean = new UecOrganizationMainComposerBean();

    private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
            .getBean("customFilesManager");

    private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
            .getBean("uecOrgOrgsManager");

    private StaffAndOutStaffManager staffAndOutStaffManager = (StaffAndOutStaffManager) ApplicationContextUtil
            .getBean("staffAndOutStaffManager");

    private UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();

    private String operType = null;

    /**
     * 操作类型
     */
    private String opType;

    private StaffAndOutStaff staffAndOutStaffLeader;

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    @Override
    public String getPortletId() {
        return super.getPortletId();
    }

    @Override
    public ThemeDisplay getThemeDisplay() {
        return super.getThemeDisplay();
    }

    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
        this.bean.getLeaderNa().addForward("onStaffAndOutStaffEditSelected", comp, "onLeaderEditSelectedResponse");
    }

    /**
     * 界面初始化
     */
    public void onCreate$uecOrganizationMainWindow() {
        bindBean();
    }

    /**
     * 监听leaderNa
     */
    public void onFocus$leaderNa() {
        operType = "orgLeaderEdit";
        Events.postEvent("onOrgLeaderEdit", this.bean.getLeaderNa().getStaffAndOutStaffListbox(), operType);
    }

    /**
     * 监听
     */
    public void onLeaderEditSelectedResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            staffAndOutStaffLeader = (StaffAndOutStaff) event.getOrigin().getData();
        }
    }

    /**
     * 页面赋值
     */
    public void bindBean() {
        opType = (String) arg.get("opType");
        if ("edit".equals(opType)) {
            uecOrgOrgs = (UecOrgOrgs) arg.get("uecOrgOrgs");
            bean.getUecOrganizationMainWindow().setTitle("组织基本信息修改");
            if (!StrUtil.isNullOrEmpty(uecOrgOrgs)) {
                PubUtil.fillBeanFromPo(uecOrgOrgs, bean);
                groupDataTranslationDisplay();
            }
        }
    }

    /**
     * 保存
     * 
     * @throws Exception
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
            ZkUtil.showError("当前登录用户不存在!", "错误提示");
            return;
        }
        String currentName = PlatformUtil.getCurrentUser().getScreenName();
        if ("edit".equals(opType)) {
            // 判断是否有值
            if (!StrUtil.isNullOrEmpty(bean.getLeaderNa().getValue())) {
                if ("无".equals(bean.getLeaderNa().getValue())) {
                    uecOrgOrgs.setLeader("无");
                } else if (!StrUtil.isNullOrEmpty(staffAndOutStaffLeader)) {
                    uecOrgOrgs.setLeader(staffAndOutStaffLeader.getPkPsndoc());
                }
            } else if (StrUtil.isNullOrEmpty(bean.getLeaderNa().getValue())) {
                uecOrgOrgs.setLeader(null);
            }
            uecOrgOrgs.setModifier(currentName);
            uecOrgOrgs.setModifiedtime(DateUtil.getCurrenDate());
            uecOrgOrgs.updateOnly();
            Events.postEvent("onOK", this.self, uecOrgOrgs);
            this.onCancel();
            Messagebox.show("更新成功！");
        }
    }

    /**
     * 取消
     */
    public void onCancel() {
        // 关闭窗口
        this.bean.getUecOrganizationMainWindow().onClose();
    }

    /**
     * 集团字段翻译
     */
    public void groupDataTranslationDisplay() {
        if (null != uecOrgOrgs) {
            // 负责人（省内）--leader
            if ("无".equals(uecOrgOrgs.getLeader())) {
                bean.getLeaderNa().setValue("无");
            } else {
                bean.getLeaderNa()
                        .setValue(staffAndOutStaffManager.getStaffAndOutStaffNameByPkPsndoc(uecOrgOrgs.getLeader()));
            }
            // 上级组织
            if (null != uecOrgOrgs.getPkFatherorg()) {
                bean.getPkFatherorg().setValue(uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecOrgOrgs.getPkFatherorg()));
            } else if ("0001A11000000000DE9G".equals(uecOrgOrgs.getPkFatherorg())) {
                bean.getPkFatherorg().setValue("中国电信股份有限公司");
            }
            // 行政划分
            bean.getGlbdef12().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef12()));
            // 机构等级
            bean.getGlbdef13().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef13()));
            // 公司类型
            bean.getGlbdef2().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef2()));
            // 公司性质
            bean.getGlbdef3().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef3()));
            // 服务区域
            bean.getGlbdef7().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef7()));
            // 单位属性
            bean.getGlbdef8().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef8()));
            // 单位层级
            bean.getGlbdef9().setValue(customFilesManager.findCustomFilesByPkdefdoc(uecOrgOrgs.getGlbdef9()));
            // 是否业务单元数据
            if (null != uecOrgOrgs.getIsbusinessunit()) {
                String isbusinessunit = uecOrgOrgs.getIsbusinessunit();
                if ("Y".equals(isbusinessunit)) {
                    bean.getIsbusinessunit().setValue("是");
                } else if ("N".equals(isbusinessunit)) {
                    bean.getIsbusinessunit().setValue("否");
                }
            }
            // 是否最新版本
            if (null != uecOrgOrgs.getIslastversion()) {
                String islastversion = uecOrgOrgs.getIslastversion();
                if ("Y".equals(islastversion)) {
                    bean.getIslastversion().setValue("是");
                } else if ("N".equals(islastversion)) {
                    bean.getIslastversion().setValue("否");
                }
            }
        }
    }
}
